﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace CP2011_Assignment
{
    public partial class EditProductUI2 : Form
    {
        int prodId;
        public EditProductUI2(int prodId)
        {
            InitializeComponent();
            this.prodId = prodId;
            labelAndTextBoxSet(new ProductionSystem());
        }

        private void labelAndTextBoxSet(IDataController dataCtrl)
        {
            ArrayList values = dataCtrl.getRecord("SELECT  Inventory.Inv_Name FROM Inventory INNER JOIN Recipe ON Inventory.Inv_ID =Recipe.Inv_ID AND Recipe.Product_ID =" + prodId);
            Label[] labelArr = new Label[5];
            labelArr[0] = lblInvNeeded1;
            labelArr[1] = lblInvNeeded2;
            labelArr[2] = lblInvNeeded3;
            labelArr[3] = lblInvNeeded4;
            labelArr[4] = lblInvNeeded5;

            TextBox[] txtBoxArr = new TextBox[5];
            txtBoxArr[0] = txtBoxQtyNeeded1;
            txtBoxArr[1] = txtBoxQtyNeeded2;
            txtBoxArr[2] = txtBoxQtyNeeded3;
            txtBoxArr[3] = txtBoxQtyNeeded4;
            txtBoxArr[4] = txtBoxQtyNeeded5;

            for (int i = 0; i < txtBoxArr.Length; i++)
            {
                txtBoxArr[i].Hide();
            }

            for (int i = 0; i < values.Count; i++)
            {
                String labelTxt = (String)values[i];
                labelArr[i].Text= labelTxt;
                txtBoxArr[i].Show();
            }

            
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to edit item with Inventory ID =" + prodId + "?", "Confirm update", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                TextBox[] textBoxArr = new TextBox[43];
                textBoxArr[0] = txtBox1;
                textBoxArr[1] = txtBox2;
                textBoxArr[2] = txtBox3;

                Label[] labelArr = new Label[5];
                labelArr[0] = lblInvNeeded1;
                labelArr[1] = lblInvNeeded2;
                labelArr[2] = lblInvNeeded3;
                labelArr[3] = lblInvNeeded4;
                labelArr[4] = lblInvNeeded5;


                if (txtBox3.Text.Length > 0)
                {

                    for (int i = 0; i < 3; i++)
                    {
                        //check whether the input is empty or not
                        if (textBoxArr[i].Text.Length != 0)
                        {
                            switch (i)
                            {
                                case 0:
                                    String editNameQuery = "UPDATE Product SET Product_Name = '" + txtBox1.Text + "'WHERE Product_ID = " + prodId;
                                    executeEdit(new ProductionSystem(), editNameQuery);
                                    break;
                                case 1:
                                    String editDescQuery = "UPDATE Product SET Product_Desc = '" + txtBox2.Text + "'WHERE Product_ID = " + prodId;
                                    executeEdit(new ProductionSystem(), editDescQuery);
                                    break;
                                case 2:
                                    {
                                        try
                                        {
                                            int qtyPerDay = Convert.ToInt32(txtBox3.Text);
                                            String editQtyQuery = "UPDATE Product SET Product_QtyPerDay = " + qtyPerDay + "WHERE Product_ID = " + prodId;
                                            executeEdit(new ProductionSystem(), editQtyQuery);
                                            for (i = 0; i < labelArr.Length; i++)
                                            {
                                                String editQuery = "UPDATE RECIPE SET Recipe_QtyPerDay = " 
                                                                    + qtyPerDay + "* (SELECT Recipe_QtyPerPiece FROM Recipe WHERE Recipe.Product_ID = " 
                                                                    + prodId + " AND Recipe.Inv_ID = (SELECT Inv_ID FROM Inventory WHERE Inv_Name = '" 
                                                                    + labelArr[i].Text + "'))" + "WHERE Product_ID = " + prodId + " AND Inv_ID = (SELECT Inv_ID FROM Inventory WHERE Inv_Name = '" 
                                                                    + labelArr[i].Text + "')";
                                                executeEdit(new ProductionSystem(), editQuery);
                                            }
                                            break;
                                        }
                                        catch
                                        {
                                            MessageBox.Show("Input format is incorrect, please insert only integer value QUANTITY PER DAY", "Incorrect Input Format", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                            
                                            break;
                                        }
                                    }//end case2
                            }
                        }//end if
                    }//end for
                    MessageBox.Show("Success updating Product details", "Success!" , MessageBoxButtons.OK, MessageBoxIcon.Information);
                }//end if

                else
                {
                    MessageBox.Show("You must enter new product QUANTITIY PER DAY field", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.Show();
                }
                TextBox[] qtyNeedArr = new TextBox[5];
                qtyNeedArr[0] = txtBoxQtyNeeded1;
                qtyNeedArr[1] = txtBoxQtyNeeded2;
                qtyNeedArr[2] = txtBoxQtyNeeded3;
                qtyNeedArr[3] = txtBoxQtyNeeded4;
                qtyNeedArr[4] = txtBoxQtyNeeded5;

                
                
//EDIT RECIPE
                if (txtBox3.Text.Length > 0)
                {
                    for (int i = 0; i < 5; i++)
                    {
                        int qtyNeedPerDay;
                        //check if the input is empty or not
                        if (qtyNeedArr[i].Text.Length != 0 && !qtyNeedArr[i].Text.Equals(" "))
                        {
                            try
                            {

                                qtyNeedPerDay = Convert.ToInt32(txtBox3.Text) * Convert.ToInt32(qtyNeedArr[i].Text);
                                String editRecipeQuery = "UPDATE Recipe SET Recipe_QtyPerDay = "
                                                        + qtyNeedPerDay
                                                        + "WHERE Recipe.Inv_ID = (SELECT Inventory.Inv_ID FROM Inventory WHERE Inventory.Inv_Name = '"
                                                        + labelArr[i].Text
                                                        + "') AND Product_ID = "
                                                        + prodId;
                                executeEdit(new ProductionSystem(), editRecipeQuery);
                                String editQuery2 = "UPDATE Recipe SET Recipe_QtyPerPeice = "
                                                     + Convert.ToInt32(qtyNeedArr[i].Text) 
                                                     + "WHERE Recipe.Inv_ID = (SELECT Inventory.Inv_ID FROM Inventory WHERE Inventory.Inv_Name = '"
                                                        + labelArr[i].Text
                                                        + "') AND Product_ID = "
                                                        + prodId;

                                //execute update
                            }
                            catch
                            {
                                MessageBox.Show("Input format is incorrect, please insert only integer value QUANTITY NEEDED", "Incorrect Input Format", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                            }
                        }
                        else
                        {
                            //do not update
                        }                  
                    }
                    MessageBox.Show("Success updating Recipe details", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                this.Hide();
                this.Owner.Show();
                this.Dispose();
            }

        }
        private void executeEdit(IDataController dataCtrl, String editQuery)
        {                    
                dataCtrl.editRecord(editQuery, prodId);
                
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Hide();
            this.Owner.Show();
            this.Dispose();
        }
    }
}
